package com.shuoyue.ycgk.ui.common;

import com.shuoyue.ycgk.base.BaseModel;
import com.shuoyue.ycgk.base.BasePresenter;
import com.shuoyue.ycgk.base.BaseResult;
import com.shuoyue.ycgk.base.BaseView;
import com.shuoyue.ycgk.entity.Type;
import com.shuoyue.ycgk.net.RetrofitClient;
import com.shuoyue.ycgk.net.base.ApiSubscriber;
import com.shuoyue.ycgk.net.gsondefaultadapter.Optional;

import java.util.List;

import io.reactivex.Observable;

public interface CommonModuleContract {
    interface View extends BaseView {
        void setBaseType(List<Type> baseTypes);

        void setTabType(List<Type> type);

    }

    class Presenter extends BasePresenter<View> {
        Model model = new Model();

        /**
         * questionType 1;试卷  2:课程,3图书,4资讯
         *
         * @param type
         */
        public void getBaseModels(int type) {
            int pid = 11;
            switch (type) {
                case 1:
                    pid = 11;
                    break;
                case 2:
                    pid = 21;
                    break;
                case 3:
                    pid = 31;
                    break;
                case 4:
                    pid = 41;
                    break;
            }
            apply(model.getBaseType(pid))
                    .subscribe(new ApiSubscriber<Optional<List<Type>>>(mView, this) {
                        @Override
                        public void onNext(Optional<List<Type>> listOptional) {
                            super.onNext(listOptional);
                            mView.setBaseType(listOptional.getIncludeNull());
                        }
                    });
        }

        public void getChildType(int typeId) {
            apply(model.getBaseType(typeId))
                    .subscribe(new ApiSubscriber<Optional<List<Type>>>(mView, this) {
                        @Override
                        public void onNext(Optional<List<Type>> listOptional) {
                            super.onNext(listOptional);
                            mView.setTabType(listOptional.getIncludeNull());
                        }
                    });
        }


    }

    class Model extends BaseModel {
        Observable<BaseResult<List<Type>>> getBaseType(int id) {
            return RetrofitClient.getInstance().getApi().moduleList(id);
        }

    }
}
